package com.fly.sms.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.fly.sms.util.C;
import com.fly.sms.util.JsonHttpClient;
import com.fly.sms.util.StringUtil;

/**
 * @author chenjr
 * 
 */
public class CustomerService extends IService{
	public static List<Map<String, Object>> cache;

	public static boolean pay(String Id,String CustomerId,String PayAmount,String Remark) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("act", C.Act.Customer_OnlinePay);
		params.put("Id", Id);
		params.put("CustomerId", CustomerId);
		params.put("PayAmount", PayAmount);
		params.put("Remark", Remark);
		JSONObject json = JsonHttpClient.post(C.serviceUrl, params);
		if(json==null){
			return false;
		}
		//Log.d("json", json.toString());
		String responseCode;
		try {
			responseCode = json.getString("responseCode");
			if("200".equals(responseCode)){
				Boolean responseData=json.getBoolean("responseData");
				return responseData;
			}else{//�쳣����
				return false;
			}
		} catch (JSONException e) {
			return false;
		}
	}
	
	public static List<Map<String, Object>> searchCashPay(String CustomerId,String pNo,String pSize) {
		cache = new ArrayList<Map<String, Object>>();
		Map<String, String> params = new HashMap<String, String>();
		params.put("act", C.Act.Customer_searchCashPay);
		params.put("CustomerId", CustomerId);
		params.put("pNo", pNo);
		params.put("pSize", pSize);
		JSONObject json = JsonHttpClient.post(C.serviceUrl, params);
		if(json==null){
			return null;
		}
		//Log.d("json", json.toString());
		String responseCode;
		try {
			responseCode = json.getString("responseCode");
			if("200".equals(responseCode)){
				JSONArray responseData=json.getJSONArray("responseData");
				int size = responseData.length();
				for (int i = 0; i < size; i++) {
					JSONObject jo;
					try {
						jo = responseData.getJSONObject(i);
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("Id", jo.getString("Id"));
						map.put("CustomerId", jo.getString("CustomerId"));
						map.put("EmployeeId", jo.getString("EmployeeId"));
						map.put("PayAmount", jo.getString("PayAmount"));
						map.put("ReceiveAmount", jo.getString("ReceiveAmount"));
						map.put("CreditsAmount", jo.getString("CreditsAmount"));
						map.put("CreateTime", jo.getString("CreateTime"));
						map.put("Remark", jo.getString("Remark"));
						cache.add(map);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}else{//�쳣����
				String responseData=json.getString("responseData");
				Log.d("searchCashPay",responseData);
				return null;
			}
		} catch (JSONException e) {
			Log.d("searchCashPay",e.getMessage());
			return null;
		}
		return cache;
	}
	
	public static List<Map<String, Object>> searchCardPay(String CustomerId,String pNo,String pSize) {
		cache = new ArrayList<Map<String, Object>>();
		Map<String, String> params = new HashMap<String, String>();
		params.put("act", C.Act.Customer_searchCardPay);
		params.put("CustomerId", CustomerId);
		params.put("pNo", pNo);
		params.put("pSize", pSize);
		JSONObject json = JsonHttpClient.post(C.serviceUrl, params);
		if(json==null){
			return null;
		}
		//Log.d("json", json.toString());
		String responseCode;
		try {
			responseCode = json.getString("responseCode");
			if("200".equals(responseCode)){
				JSONArray responseData=json.getJSONArray("responseData");
				int size = responseData.length();
				for (int i = 0; i < size; i++) {
					JSONObject jo;
					try {
						jo = responseData.getJSONObject(i);
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("Id", jo.getString("Id"));
						map.put("CustomerId", jo.getString("CustomerId"));
						map.put("CardId", jo.getString("CardId"));
						map.put("PayAmount", jo.getString("PayAmount"));
						map.put("CreditsAmount", jo.getString("CreditsAmount"));
						map.put("CreateTime", jo.getString("CreateTime"));
						map.put("Remark", jo.getString("Remark"));
						cache.add(map);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}else{//�쳣����
				String responseData=json.getString("responseData");
				Log.d("searchCashPay",responseData);
				return null;
			}
		} catch (JSONException e) {
			Log.d("searchCashPay",e.getMessage());
			return null;
		}
		return cache;
	}
	
	
	public static boolean login(String name, String pwd,String checkCode) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("act", C.Act.Customer_login);
		params.put("LoginName", name);
		params.put("LoginPass", pwd);
		params.put("CheckCode", checkCode);
		JSONObject jo = JsonHttpClient.post(C.serviceUrl, params);
		if(jo==null){
			return false;
		}
		String responseCode;
		try {
			responseCode = jo.getString("responseCode");
			if("200".equals(responseCode)){
				JSONObject responseData=jo.getJSONObject("responseData");
				String loginState=responseData.getString("loginstate");
				if(StringUtil.boolValue(loginState)){
					String sessionkey=responseData.getString("sessionkey");
					return true;
				}else{
					return false;
				}
			}else{//�쳣����
				String responseData=jo.getString("responseData");
				Log.d("login",responseData);
				return false;
			}
		} catch (JSONException e) {
			Log.d("login",e.getMessage());
			return false;
		}
	}
	
	public static boolean chgpass(String LoginName, String LoginPass,String NewPass) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("act", C.Act.Customer_changePass);
		params.put("LoginName", LoginName);
		params.put("LoginPass", LoginPass);
		params.put("NewPass", NewPass);
		JSONObject json = JsonHttpClient.post(C.serviceUrl, params);
		if(json==null){
			return false;
		}
		String responseCode;
		try {
			responseCode = json.getString("responseCode");
			if ("200".equals(responseCode)) {
				boolean responseData = json.getBoolean("responseData");
				return responseData;
			} else {// �쳣����
				String responseData = json.getString("responseData");
				Log.d("chgpass", responseData);
				return false;
			}
		} catch (JSONException e) {
			Log.d("chgpass", e.getMessage());
			return false;
		}
	}
	
	public static Map<String, Object> get(String LoginName) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("act", C.Act.Customer_get);
		params.put("LoginName", LoginName);
		JSONObject json = JsonHttpClient.post(C.serviceUrl, params);
		if(json==null){
			return null;
		}
		//Log.d("json", json.toString());
		String responseCode;
		try {
			responseCode = json.getString("responseCode");
			if("200".equals(responseCode)){
				JSONArray responseData=json.getJSONArray("responseData");
				int size = responseData.length();
				for (int i = 0; i < size; i++) {
					JSONObject jo;
					try {
						jo = responseData.getJSONObject(i);
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("Id", jo.getString("Id"));
						map.put("LoginName", jo.getString("LoginName"));
						map.put("LoginPass", jo.getString("LoginPass"));
						map.put("Name", jo.getString("Name"));
						map.put("IsVerifyOk", jo.getString("IsVerifyOk"));
						map.put("PayType", jo.getString("PayType"));
						map.put("MaxSendNumber", jo.getString("MaxSendNumber"));
						map.put("Price", jo.getString("Price"));
						map.put("FinanceBalance", jo.getString("FinanceBalance"));
						map.put("ReceiveFinanceBalance", jo.getString("ReceiveFinanceBalance"));
						map.put("MsgBalance", jo.getString("MsgBalance"));
						map.put("Limit", jo.getString("Limit"));
						map.put("MsgUnBalance", jo.getString("MsgUnBalance"));
						map.put("IsActive", jo.getString("IsActive"));
						map.put("FlagLock", jo.getString("FlagLock"));
						map.put("AllowSendTimeFrom", jo.getString("AllowSendTimeFrom"));
						map.put("AllowSendTimeTo", jo.getString("AllowSendTimeTo"));
						map.put("CreateTime", jo.getString("CreateTime"));
						map.put("IsBatch", jo.getString("IsBatch"));
						return map;
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				return null;
			}else{//�쳣����
				String responseData=json.getString("responseData");
				Log.d("notify",responseData);
				return null;
			}
		} catch (JSONException e) {
			Log.d("notify",e.getMessage());
			return null;
		}
	}
}
